package twelfth.BufferedReader类.实例操作;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class InputData {
    private BufferedReader bf = null;

    public InputData() {
        this.bf = new BufferedReader(new InputStreamReader(System.in));
    }

/*    public String getString(String info) {
        String temp = null;
        System.out.println(info);
        try {
            temp = this.bf.readLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return temp;
    }

    public int getInt(String info, String err) {
        int temp = 0;
        String s = null;
        boolean flag = true;
        while (flag) {
            s = this.getString(info);
            if (s.matches("^\\d+$")) {
                temp = Integer.parseInt(s);
                flag = false;
            }
        }
        return temp;
    }

    public float getFloat(String info, String err) {
        float temp = 0;
        String s = null;
        boolean flag = true;
        while (flag) {
            if (s.matches("^\\d+.?\\d+$")) {
                temp = Float.parseFloat(s);
                flag = false;
            }
        }
        return temp;
    }

    public Date getDate(String info, String err) {
        Date d = null;
        String s = null;
        boolean flag = true;
        while (flag) {
            s = this.getString(info);
            if (s.matches("^\\d{4}+\\d{2}-\\d{2}$")) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    d = sdf.parse(s);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return d;
    }*/
    public String getString(String info) throws IOException {
        String temp;
        System.out.println(info);
        temp = this.bf.readLine();
        return temp;
    }

    public int getInt(String info, String err) throws IOException {
        int temp = 0;
        String s=null;
        boolean flag = true;
        while (flag) {
            s = this.getString(info);
            if (s.matches("^\\d+$")) {
                temp = Integer.parseInt(s);
                flag = false;
            } else {
                System.out.println(err);
            }
        }
        return  temp;
    }
    public float getFloat(String info,String err) throws IOException {
        float temp = 0;
        boolean flag = true;
        String s ;
        while (flag){
            s=this.getString(info);
            if(s.matches("^\\d+.?\\d+$")){
                temp=Float.parseFloat(s);
                flag=false;
            }else{
                System.out.println(err);
            }
        }
        return temp;
    }
    public Date getDate(String info, String err) throws IOException, ParseException {
        String s = null;
        Date d  = null;
        boolean flag =true;
        while (flag){
            s=this.getString(info);
            if(s.matches("^\\d{4}-\\d{2}-\\d{2}$")){
                SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
                  d = sdf.parse(s);
                  flag = false;
            }else{
                System.out.println(err);
            }
        }
        return d;
    }
}
/**
 * 上面的程序可以实现：整数，小数，字符串，日期类型数据的输入
 * 在得到日期类型时候用SimpleDateFormat类，并指定日期转换的模板
 */